package cn.zy.shardbatisdemo.tools;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author zhagnying
 * @description
 * @date 2019/9/11
 */
@Component
public class SpringContextAware implements ApplicationContextAware {
    private static ApplicationContext applicationContexts;//使用静态，让它存储下来,通过类型即可获取

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContexts = applicationContext;
    }

    public static <T> T getBean(String name){
        return (T) applicationContexts.getBean(name);
    }

}
